package org.example.product.controller;

import java.util.List;

import org.example.core.utils.R;
import org.example.core.valid.group.AddGroup;
import org.example.core.valid.group.UpdateGroup;
import org.example.product.entity.CategoryEntity;
import org.example.product.service.CategoryService;
import org.example.product.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@RestController
@RequestMapping("/pms/backend/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 树数据
     */
    @GetMapping("/tree")
    public R<List<CategoryVo>> list(){
        List<CategoryVo> tree = categoryService.tree();
        return R.ok(tree);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R<CategoryEntity> info(@PathVariable("id") Long id){
        CategoryEntity entity = categoryService.queryCategoryInfoById(id);
        return R.ok(entity);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R<?> save(@Validated(value = {AddGroup.class}) @RequestBody CategoryEntity categoryEntity){
        return categoryService.saveCategoryInfo(categoryEntity)>0? R.ok():R.error();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R<?> update(@Validated(value = {UpdateGroup.class}) @RequestBody CategoryEntity categoryEntity){
        return categoryService.editCategoryInfo(categoryEntity)>0? R.ok():R.error();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R<?> delete(@RequestBody List<Long> ids){
        return categoryService.deletes(ids);
    }
}
